package com.darker.yygh.sms.service.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.darker.yygh.sms.service.SmsService;
import com.darker.yygh.sms.utils.ConstantPropertiesUtils;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.httpclient.HTTPException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

@Service
public class SmsSreviceImpl implements SmsService {


    //发送验证码
    @Override
    public boolean send(String phone, String code) {
        String [] parm={code};
            try {

                SmsSingleSender ssender = new SmsSingleSender(ConstantPropertiesUtils.APPID, ConstantPropertiesUtils.APPKEY);
                ssender.sendWithParam("86", phone,
                        ConstantPropertiesUtils.TEMPLATEID, parm,"Zzzzero1公众号", "", "");
            } catch (HTTPException e) {
                // HTTP 响应码错误
                e.printStackTrace();
            } catch (JSONException e) {
                // JSON 解析错误
                e.printStackTrace();
            } catch (IOException e) {
                // 网络 IO 错误
                e.printStackTrace();
            }
            return true;

    }

    //Mq发送短信
    @Override
    public boolean send(MsmVo msmVo) {
        String phone = msmVo.getPhone();
        if(!StringUtils.isEmpty(phone)){
            boolean flag = this.send(phone, msmVo.getParam());
            return flag;

        }
        return false;
    }
    private boolean send(String phone, Map<String,Object> map) {
        String s = JSONObject.toJSONString(map);
        String parm[]={s};


        try {

            SmsSingleSender ssender = new SmsSingleSender(ConstantPropertiesUtils.APPID, ConstantPropertiesUtils.APPKEY);
            ssender.sendWithParam("86", phone,
                    ConstantPropertiesUtils.TEMPLATEID, parm,"Zzzzero1公众号", "", "");
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
        }
        return true;

    }
}
